function ti = setlayeralpha(ti, l, a)
% transimg::setlayeralpha  - set alpha channel of a layer of an image

% global variables for storage
global tiobj ...
       tiobjlup;
   
% check arguments
lup = find(tiobjlup == ti.L);
if numel(lup) ~= 1
    error( ...
        'transimg:ObjectRemoved', ...
        'Object removed from global storage.' ...
    );
end
if nargin < 3 || ...
   ~isa(l, 'double') || ...
    numel(l) ~= 1 || ...
    isinf(l) || ...
    isnan(l) || ...
    l < 1 || ...
    l > numel(tiobj(lup).Layer) || ...
   ~isa(a, 'double') || ...
   (numel(a) ~= 1 && ...
    (ndims(a) ~= 2 || ...
     size(a, 1) ~= tiobj(lup).Height || ...
     size(a, 2) ~= tiobj(lup).Width))
    error( ...
        'transimg:InvalidCall', ...
        'Invalid call to transimg::addlayer.' ...
    );
end
l = floor(real(l));

% reset IsRendered flag
tiobj(lup).IsRendered = false;

% set layer alpha
tiobj(lup).Layer(l).Alpha = a;
